library(foreign)
#library(reshape)
library(tidyverse)
library(ggplot2)
library(haven)


se <- function(x) sd(x)/sqrt(length(x))


#####EDUCATION#####
data_ed <- read_dta('education_data.dta')


#### SPLIT ON PID ###
data_ed$pid7 <- data_ed$Q58
data_ed$pid7[data_ed$Q56==3 & data_ed$Q59==1]<-3
data_ed$pid7[data_ed$Q56==3 & data_ed$Q59==3]<- 4
data_ed$pid7[data_ed$Q56==3 & data_ed$Q59==2] <-5
data_ed$pid7[data_ed$Q57==2]<- 6
data_ed$pid7[data_ed$Q57==1] <-7

data_ed$rep[data_ed$pid7>4] <- 1
data_ed$rep[data_ed$pid7<=4] <- 0

data_ed$dem[data_ed$pid7>4] <- 0
data_ed$dem[data_ed$pid7<4] <- 1


REPed <- subset(data_ed, pid7>4)
DEMed <- subset(data_ed, pid7<4)

gen <- data.frame(
  x = as.factor(c(rep(c(1:2),2),rep(c(1:2),2))),
  cond = as.factor(c(1,1,1,1,1,1,1,1)),
  dv = as.factor(c(rep("Agree",4),rep("Vote",4))),
  rr_def = c(rep("Republican",2),rep("Democrat",2),rep("Republican",2),rep("Democrat",2)),
  y = c(mean(na.omit(REPed$AgreeCandidate[REPed$treat==1])),
        mean(na.omit(REPed$AgreeCandidate[REPed$treat==3])),
        mean(na.omit(DEMed$AgreeCandidate[DEMed$treat==1])),
        mean(na.omit(DEMed$AgreeCandidate[DEMed$treat==3])),
        mean(na.omit(REPed$vote[REPed$treat==1])),
        mean(na.omit(REPed$vote[REPed$treat==3])),
        mean(na.omit(DEMed$vote[DEMed$treat==1])),
        mean(na.omit(DEMed$vote[DEMed$treat==3]))),
  se = c(se(na.omit(REPed$AgreeCandidate[REPed$treat==1])),
         se(na.omit(REPed$AgreeCandidate[REPed$treat==3])),
         se(na.omit(DEMed$AgreeCandidate[DEMed$treat==1])),
         se(na.omit(DEMed$AgreeCandidate[DEMed$treat==3])),
         se(na.omit(REPed$vote[REPed$treat==1])),
         se(na.omit(REPed$vote[REPed$treat==3])),
         se(na.omit(DEMed$vote[DEMed$treat==1])),
         se(na.omit(DEMed$vote[DEMed$treat==3])))
)



g <- ggplot(gen,aes(x=dv,y=y,ymin=y-(1.96*se),ymax=y+(1.96*se),width= 0.5, colour = x, shape=x))
g+  coord_cartesian(ylim=c(0,1)) +
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5), 
                     labels=c("White Derogator","Black Derogator")) +  
  scale_color_manual(name="Treatment",values=c("grey45","black"), 
                     labels=c("White Derogator","Black Derogator")) + 
  scale_fill_manual(values=c("grey45","black")) +
  #scale_x_discrete(breaks=c("1","2"),labels=c("White \nCandidate","Black \nCandidate"))
  coord_flip() +
  #geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( x = "Dependent Variable", y= "Mean", color = "Category") +
  facet_grid(.~rr_def,labeller = label_value,space="free")+
  theme_bw()
ggsave(file="educationPID_figure.pdf",device="pdf",width = 175, height = 125, units = "mm" )



RRlow.ed <- subset(data_ed, symrace<.5)
RRhi.ed <- subset(data_ed, symrace>.5)

gen <- data.frame(
  x = as.factor(c(rep(c(1:2),2),rep(c(1:2),2))),
  cond = as.factor(c(1,1,1,1,1,1,1,1)),
  dv = as.factor(c(rep("Agree",4),rep("Vote",4))),
  rr_def = c(rep("High RR",2),rep("Low RR",2),rep("High RR",2),rep("Low RR",2)),
  y = c(mean(na.omit(RRhi.ed$AgreeCandidate[RRhi.ed$treat==1])),
        mean(na.omit(RRhi.ed$AgreeCandidate[RRhi.ed$treat==3])),
        mean(na.omit(RRlow.ed$AgreeCandidate[RRlow.ed$treat==1])),
        mean(na.omit(RRlow.ed$AgreeCandidate[RRlow.ed$treat==3])),
        mean(na.omit(RRhi.ed$vote[RRhi.ed$treat==1])),
        mean(na.omit(RRhi.ed$vote[RRhi.ed$treat==3])),
        mean(na.omit(RRlow.ed$vote[RRlow.ed$treat==1])),
        mean(na.omit(RRlow.ed$vote[RRlow.ed$treat==3]))),
  se = c(se(na.omit(RRhi.ed$AgreeCandidate[RRhi.ed$treat==1])),
         se(na.omit(RRhi.ed$AgreeCandidate[RRhi.ed$treat==3])),
         se(na.omit(RRlow.ed$AgreeCandidate[RRlow.ed$treat==1])),
         se(na.omit(RRlow.ed$AgreeCandidate[RRlow.ed$treat==3])),
         se(na.omit(RRhi.ed$vote[RRhi.ed$treat==1])),
         se(na.omit(RRhi.ed$vote[RRhi.ed$treat==3])),
         se(na.omit(RRlow.ed$vote[RRlow.ed$treat==1])),
         se(na.omit(RRlow.ed$vote[RRlow.ed$treat==3])))
)




g <- ggplot(gen,aes(x=dv,y=y,ymin=y-(1.96*se),ymax=y+(1.96*se),width= 0.5, colour = x, shape=x))
g+  coord_cartesian(ylim=c(0,1)) +
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5), 
                     labels=c("White Candidate","Black Candidate")) +  
  scale_color_manual(name="Treatment",values=c("grey45","black"), 
                     labels=c("White Candidate","Black Candidate")) + 
  scale_fill_manual(values=c("grey45","black")) +
  #scale_x_discrete(breaks=c("1","2"),labels=c("White \nCandidate","Black \nCandidate"))
  coord_flip() +
  #geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( x = "Dependent Variable", y= "Mean", color = "Category") +
  facet_grid(.~rr_def,labeller = label_value,space="free")+
  theme_bw()
ggsave(file="educationRR_figure.pdf",device="pdf",width = 175, height = 125, units = "mm" )




############################ WELFARE #######################################

data_wel <- read_dta('welfare_data.dta')


#### SPLIT ON PID ###
data_wel$pid7 <- data_wel$Q58
data_wel$pid7[data_wel$Q56==3 & data_wel$Q59==1]<-3
data_wel$pid7[data_wel$Q56==3 & data_wel$Q59==3]<- 4
data_wel$pid7[data_wel$Q56==3 & data_wel$Q59==2] <-5
data_wel$pid7[data_wel$Q57==2]<- 6
data_wel$pid7[data_wel$Q57==1] <-7

data_wel$dem[data_wel$pid7>4]<-0
data_wel$dem[data_wel$pid7<4]<- 1

data_wel$rep[data_wel$pid7>4]<-1
data_wel$rep[data_wel$pid7<=4]<- 0

REPwel <- subset(data_wel, pid7>4)
DEMwel <- subset(data_wel, pid7<4)

gen <- data.frame(
  x = as.factor(c(rep(c(1:2),2),rep(c(1:2),2))),
  cond = as.factor(c(1,1,1,1,1,1,1,1)),
  dv = as.factor(c(rep("Agree",4),rep("Vote",4))),
  rr_def = c(rep("Republican",2),rep("Democrat",2),rep("Republican",2),rep("Democrat",2)),
  y = c(mean(na.omit(REPwel$agree[REPwel$treatment==5])),
        mean(na.omit(REPwel$agree[REPwel$treatment==6])),
        mean(na.omit(DEMwel$agree[DEMwel$treatment==5])),
        mean(na.omit(DEMwel$agree[DEMwel$treatment==6])),
        mean(na.omit(REPwel$vote[REPwel$treatment==5])),
        mean(na.omit(REPwel$vote[REPwel$treatment==6])),
        mean(na.omit(DEMwel$vote[DEMwel$treatment==5])),
        mean(na.omit(DEMwel$vote[DEMwel$treatment==6]))),
  se = c(se(na.omit(REPwel$agree[REPwel$treatment==5])),
         se(na.omit(REPwel$agree[REPwel$treatment==6])),
         se(na.omit(DEMwel$agree[DEMwel$treatment==5])),
         se(na.omit(DEMwel$agree[DEMwel$treatment==6])),
         se(na.omit(REPwel$vote[REPwel$treatment==5])),
         se(na.omit(REPwel$vote[REPwel$treatment==6])),
         se(na.omit(DEMwel$vote[DEMwel$treatment==5])),
         se(na.omit(DEMwel$vote[DEMwel$treatment==6])))
)



g <- ggplot(gen,aes(x=dv,y=y,ymin=y-(1.96*se),ymax=y+(1.96*se),width= 0.5, colour = x, shape=x))
g+  coord_cartesian(ylim=c(0,1)) +
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5), 
                     labels=c("White Candidate","Black Candidate")) +  
  scale_color_manual(name="Treatment",values=c("grey45","black"), 
                     labels=c("White Candidate","Black Candidate")) + 
  scale_fill_manual(values=c("grey45","black")) +
  #scale_x_discrete(breaks=c("1","2"),labels=c("White \nCandidate","Black \nCandidate"))
  coord_flip() +
  #geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( x = "Dependent Variable", y= "Mean", color = "Category") +
  facet_grid(.~rr_def,labeller = label_value,space="free")+
  theme_bw()
ggsave(file="welfarePID_figure.pdf",device="pdf",width = 175, height = 125, units = "mm" )




